#ifndef __SPINLOCK_H__
#define __SPINLOCK_H__

#include <common.h>

typedef struct spinlock {
  int locked;
} spinlock_t;

static inline void lockinit(spinlock_t *lk) {
  lk->locked = 0;
}

static inline void lock(spinlock_t *lk) {
  while (atomic_xchg(&lk->locked, 1) == 1) ;
}

static inline void unlock(spinlock_t *lk) {
  atomic_xchg(&lk->locked, 0);
}

static inline bool __intr_save() {
  if (ienabled()) {
    iset(false);
    return true;
  }
  return false;
}

static inline void __intr_restore(bool flag) {
  if (flag) {
    iset(true);
  }
}

#define local_irq_save(x)	do { x = __intr_save(); } while (0)
#define local_irq_restore(x) do { __intr_restore(x); } while (0)

#endif